home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / File / splitter / splitter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-12  |  3.8 KB  |  133 lines

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <sys/param.h>
  4. #define MAX_PREFIX_LENGTH 6
  5. #define MAX_FILENAME_LENGTH 70        /* change here for longer filenames */
  6.  
  7. void main(int argc,char *argv[])
  8. {
  9.   char filename[MAXPATHLEN+1];
  10.   char from_line[2048];
  11.   char subject_line[2048];
  12.   char copy_filename[MAX_FILENAME_LENGTH+1];    /* max limited for tar */
  13.   char buffer[2048];
  14.   char prefix[MAX_PREFIX_LENGTH+2];
  15.   char from[20];
  16.   char subject[20], subject1[20];
  17.   char reply[20],reply1[20], reply2[20], reply3[20];
  18.   int i,j,subject_length,prefix_length,stop_flag,total,offset,max_length;
  19.   FILE *infile,*outfile;
  20.   
  21.   /* load test header strings, account for missing spaces, etc. */
  22.   sprintf(from, "From:");
  23.   sprintf(subject, "Subject: ");
  24.   sprintf(subject1, "Subject:");
  25.   sprintf(reply, "Subject: Re: ");
  26.   sprintf(reply1, "Subject:Re:");
  27.   sprintf(reply2, "Subject: Re:");
  28.   sprintf(reply3, "Subject:Re: ");
  29.   
  30.   if(argc > 2){          /* check for correct no. of arguments */
  31.   /* get desired prefix for all generated files */
  32.   strcpy(prefix, argv[1]);
  33.   
  34.   for(i=2; i<argc; i++){    /* loop until all files have been processed */
  35.     strcpy(filename, argv[i]);
  36.     
  37.     /* open archived file for reading */
  38.     if((infile = fopen(filename, "r")) != NULL){
  39.       total = 0;
  40.       printf("Successfully opened %s for input...now splitting\n", filename);
  41.       stop_flag = 0;
  42.       fgets(from_line, 2047, infile); /* read first line */
  43.  
  44.       /* continue reading until an error or EOF */
  45.       do{
  46.         copy_filename[0] = '\0';    /* reset filename */
  47.         /* add prefix */
  48.     prefix_length = strlen(prefix);
  49.     for(j=0; j<prefix_length; j++)
  50.       copy_filename[j] = prefix[j];
  51.     copy_filename[j] = '\0';    /* null termination */
  52.     strcat(copy_filename, "_");    /* concatenate underscore */
  53.  
  54.         fgets(subject_line, 2047, infile); /* read subject line */
  55.         subject_length = strlen(subject_line);
  56.     
  57.     /* the test order is important here */
  58.     if(strncmp(subject_line, subject1, 8) == 0){
  59.       offset = 8;
  60.     }
  61.     if(strncmp(subject_line, subject, 9) == 0){
  62.       offset = 9;
  63.     }
  64.     if(strncmp(subject_line, reply2, 12) == 0){
  65.       offset = 12;
  66.     }
  67.     if(strncmp(subject_line, reply, 13) == 0){
  68.       offset = 13;
  69.     }
  70.     if(strncmp(subject_line, reply1, 11) == 0){
  71.       offset = 11;
  72.     }
  73.     if(strncmp(subject_line, reply3, 12) == 0){
  74.       offset = 12;
  75.     }
  76.  
  77.     
  78.     /* replace 'bad' characters with a '_' */
  79.     max_length = MAX_FILENAME_LENGTH- prefix_length+ offset- 1;
  80.     if((subject_length- 1+ prefix_length- offset) < MAX_FILENAME_LENGTH)
  81.       max_length = subject_length-1;
  82.     
  83.         for(j=offset; j<max_length; j++){
  84.       if(subject_line[j] != '.' && subject_line[j] != '[' && 
  85.          subject_line[j] != ']' && subject_line[j] != ' ' &&
  86.          subject_line[j] != '/')
  87.         strncat(copy_filename, &subject_line[j], 1);
  88.       else
  89.         strcat(copy_filename, "_");
  90.     }
  91.           
  92.     /* open an output file with prefix+modified_subject as name */
  93.     /* note this appends to a file with identical subject line */
  94.     outfile = fopen(copy_filename, "a");
  95.     total++;    
  96.  
  97.     /* write the from line, subject lines, and content to outfile */
  98.     fputs(from_line, outfile);
  99.     fputs(subject_line, outfile);
  100.     
  101.     for(;;){
  102.       if(fgets(buffer, 2047, infile) != NULL){
  103.         if(strncmp(buffer, from, 5) != 0){
  104.           fputs(buffer, outfile);
  105.         }
  106.         else{
  107.           fclose(outfile);
  108.           strcpy(from_line, buffer);
  109.           break;        /* next message found */
  110.         }
  111.       }
  112.       else{
  113.             stop_flag = 1;    /* EOF reached */
  114.         fclose(infile);
  115.         printf("  -- %d messages.\n", total);
  116.         break;
  117.       }
  118.     }
  119.       }
  120.       while(stop_flag == 0);    /* continue until EOF */
  121.     }
  122.     else{
  123.       printf("Error opening %s for input\n");
  124.       printf("  -- continuing with next file...\n");
  125.     }
  126.   }
  127.   }
  128.   else{
  129.     printf("Form of command:\n");
  130.     printf("splitter prefix filename1 filename2 ...\n");
  131.     printf("(prefix can be up to 6 characters)\n");
  132.   }
  133. }